package com.briup.product_source.service.Impl;

import com.briup.product_source.bean.Basic.FenceHouse;
import com.briup.product_source.bean.Ext.FenceHouseExt;
import com.briup.product_source.dao.Ext.FenceHouseExtMapper;
import com.briup.product_source.dao.Ext.HurdlesExtMapper;
import com.briup.product_source.exception.CustomerException;
import com.briup.product_source.service.FenceHouseService;
import com.briup.product_source.util.Assert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

import static com.briup.product_source.util.ResultCode.*;

@Service
public class FenceHouseServiceImpl implements FenceHouseService {
    @Autowired
    private FenceHouseExtMapper fhExtMapper;
    @Autowired
    private HurdlesExtMapper hExtMapper;
    @Override
    public PageInfo<FenceHouse>  findByPage(int pageNum, int pageSize, String fhName) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<FenceHouse> list = fhExtMapper.selectByPage(fhName);
        return new PageInfo<>(list);
    }

    @Override
    public FenceHouseExt findById(String id) {
        FenceHouseExt fhExt = fhExtMapper.findById(id);
        return fhExt;
    }

    @Override
    public void saveOrUpdate(FenceHouse fenceHouse) {
        String fhId = fenceHouse.getFhId();
        String fhName = fenceHouse.getFhName();
        //校验参数 栏舍名是否为空
        Assert.hasText(fhName,FH_NAME_NULL);
        //校验参数 描述信息是否为空
        Assert.hasText(fenceHouse.getFhDesc(),DESC_NULL);
        if(fhId == null || "".equals(fhId.trim())){
            //新增操作
            String id = UUID.randomUUID().toString().replace("-", "");
            fenceHouse.setFhId(id);
            fhExtMapper.insert(fenceHouse);
        }else{
            //修改操作
            /*FenceHouse fHouse = fhExtMapper.selectByPrimaryKey(fhId);
            if(fHouse == null){
                throw new CustomerException(FHID_NOT_EXIT);
            }*/
            Assert.isNull(fhExtMapper.selectByPrimaryKey(fhId),FHID_NOT_EXIT);
            /*FenceHouse fHouse2 = fhExtMapper.selectByName(fhName);
            if(fHouse2 != null){
                throw new CustomerException(FH_NAME_REPEAT);
            }*/
            Assert.notNull(fhExtMapper.selectByName(fhName),FH_NAME_REPEAT);
            fhExtMapper.updateByPrimaryKey(fenceHouse);
        }
    }

    @Override
    public void deleteById(String id) {
        Assert.isNull(fhExtMapper.selectByPrimaryKey(id),FHID_NOT_EXIT);
        hExtMapper.updateFenceId(id);
        fhExtMapper.deleteByPrimaryKey(id);
    }


    //事务管理：实现当前方法中操作同时成功，同时失败
    //在sql当中使用foreach遍历不需要这个注解
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByBatch(List<String> ids) {
        fhExtMapper.deleteByBatch(ids);
    }
}
